package com.fr.demo.config;

import com.fr.demo.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启方法安全注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.csrf().disable()
//                .authorizeRequests()
//                //.antMatchers("/swagger-ui/**").permitAll()// 公开访问的资源
//                //.antMatchers("/login").permitAll()
//                .antMatchers("/selectBook/**").authenticated()           // 其他所有请求都需要认证
//                .anyRequest().permitAll()
//                //.anyRequest().authenticated()           // 其他所有请求都需要认证
//                .and()
//                .formLogin()                                 // 表单登录
//                .loginPage("/loginPage")                     // 自定义登录页面
//                .loginProcessingUrl("/login")//定义表单提交url
//                .successForwardUrl("/login-success")//自定义登录成功的页面地址
//                //.permitAll()
//                .and()
//                .logout()                                   // 退出登录
//                .permitAll();

        http.csrf().disable()
                .authorizeRequests()
                //.antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login").permitAll()                   // 自定义登录页面loginPage
 //               .loginProcessingUrl("/login").permitAll() //定义表单提交url
//                .successForwardUrl("/login-success").permitAll()
                .and()
                .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

}
